using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;

public class InvoiceItem
{
    private int invoiceItemId;
    private int invoiceId;
    private string details;
    private double quantity;
    private UnitType unitType;
    private decimal rate;

    public int InvoiceItemId
    {
        get { return invoiceItemId; }
        set { invoiceItemId = value; }
    }

    public int InvoiceId
    {
        get { return invoiceId; }
        set { invoiceId = value; }
    }

    public string Details
    {
        get { return details; }
        set { details = value; }
    }

    public double Quantity
    {
        get { return quantity; }
        set { quantity = value; }
    }

    public UnitType UnitType
    {
        get { return unitType; }
        set { unitType = value; }
    }

    public decimal Rate
    {
        get { return rate; }
        set { rate = value; }
    }

    /// <summary>
    /// Parameterless constructor for serialization.
    /// </summary>
    public InvoiceItem() { }

    public InvoiceItem(
        int invoiceItemId, 
        int invoiceId, 
        string details,
        double quantity,
        UnitType unitType,
        decimal rate)
    {
        this.invoiceItemId = invoiceItemId;
        this.invoiceId = invoiceId;
        this.details = details;
        this.quantity = quantity;
        this.unitType = unitType;
        this.rate = rate;
    }

    public static List<InvoiceItem> Fetch(int invoiceId)
    {
        List<InvoiceItem> invoiceItemList = new List<InvoiceItem>();
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.AccountsDatabase))
        {
            SqlCommand command = new SqlCommand("InvoiceItemFetch", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("invoiceId", invoiceId);

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                InvoiceItem item = new InvoiceItem(
                    (int)reader["InvoiceItemId"],
                    (int)reader["InvoiceId"],
                    (string)reader["Details"],
                    (double)reader["Quantity"],
                    (UnitType)reader["UnitTypeFlag"],
                    (decimal)reader["Rate"]);

                invoiceItemList.Add(item);
            }
            connection.Close();
        }
        return invoiceItemList;
    }
}

public enum UnitType
{
    Other, Hours, Days, Months, Years
}
